// 首页
homeApp.controller("homeIndexCtr",function($scope,$location,commonService,userService,$timeout){
	//$(".start_loading").addClass("loading");
	
	if(userService.userIsLogin()){
		$scope.userIsLogin = true;
	}else{
		$scope.userIsLogin = false;
	}

	$scope.disable = false;
	$scope.readonly = false;
	$scope.registerAble = false;
	$scope.pop = false;	//弹出报名成功
	$scope.tips =false; //分享
	$scope.achieved = false;//已经报名
	$scope.vertify = false;//成功发送验证码
	
	$scope.wrong = false;//验证码错误重新发送
	//console.log($scope.pop)

	var aid=$location.search().aid;
	var clock = null;

	if("undefined" == typeof clock || clock == null){
		clearInterval(clock);
	}

	if(aid==undefined){
		aid=2;
	}

	var formDataStr = $.param({
		aid:aid,
		phone:userService.getLocalStorage("enroll-uuid")
	})


	commonService.httpReq(indexUrl,formDataStr,function(response){
		if(response.STATUS == SUCCESS){
			var res = response.RESULT.data[0];
			res['times']=response.RESULT.times;
			$scope.colorClass=res.color;
			$scope.bannerBg=urlPic+res.bg;
			$scope.showOther1=res.other1==null?false:true;
			$scope.showOther2=res.other3==null?false:true;
			$scope.showOther3=res.other3==null?false:true;
			$scope.urlPic = urlPic+res.t_pic;
			$scope.res = res;
			
		}
	})

	$scope.sendVertify = function(){
		if(!userService.checkPhoneNum($scope.mobile.toString().replace(/\s/g,'')) || "undefined" == typeof $scope.mobile){
			alert("笨蛋，手机号输入错啦~"+$scope.mobile);
			return false;
		}
		$scope.disable = true;
		$scope.readonly = true;
		var time = 60;
		var formDataStr = $.param({
			phone:$scope.mobile.toString().replace(/\s/g,''),
		});

		commonService.httpReq(sendVertifyUrl,formDataStr,function(response){
			if(response.STATUS == SUCCESS){
				$scope.vertify = true;//发送验证码
				$timeout(function(){$scope.vertify = false;}, 2000);
				//alert("验证码已经发送到您的手机啦！");
				$scope.registerAble = true;
				clock = setInterval(function(){
					$("#certify").text("("+time+")s后获取");
					time--;
					if(time == 0){
						$scope.disable = false;
						$scope.readonly = false;
						$("#certify").text("获取验证码")
						clearInterval(clock);
					}
				},1000)
			}else{
				alert(response.ERRMSG);
				$scope.disable = false;
				$scope.readonly = false;
			}
		})
	}


	$scope.register = function(){
		if( "undefined" == typeof $scope.vcode ){
			//alert("验证码不能为空！");
			$scope.wrong = true;//验证码错误
			$timeout(function(){$scope.vertify = wrong;}, 2000);
			return false;
		}
		var formDataStr = $.param({
			phone: $scope.mobile.toString().replace(/\s/g,''),
    		vertify: $scope.vcode
    	})

		commonService.httpReq(registerUrl,formDataStr,function(response){
			if(response.STATUS == SUCCESS){
				userService.setLocalStorage("enroll-uuid",response.RESULT.phone);
				var phone = response.RESULT.phone;
				//报名活动
				var formDataStr = $.param({
					phone: response.RESULT.phone,
    				aid: aid
    			})
				commonService.httpReq(signUrl,formDataStr,function(response){
					if(response.STATUS == SUCCESS){
						var formDataStr = $.param({
							phone: phone
						});
						commonService.httpReq(getTimesUrl,formDataStr,function(response){
							if(response.STATUS == SUCCESS){
								var activities = response.RESULT;
								var actList = [];
								$.each(activities.record,function(index,value){
									var temp = {};
									temp['num'] = index;
									temp['term'] = value;
									actList.push(temp);
								})
								activities.record = actList;
								$scope.activities = activities;
								$scope.pop = true;
							}else{
								alert(response.ERRMSG);
							}
						})
					}else{
						$scope.achieved = true;
						//alert(response.ERRMSG);
					}
				});
				$location.path("/home/index");
			}else{
				//alert(response.ERRMSG);
				$scope.wrong = true;//验证码错误
				$timeout(function(){$scope.wrong = false;}, 2000);
			}
		})
	}

	$scope.tipshare = function(){
		$scope.tips=true;
		//$scope.pop = false;
	}

	$scope.acclose = function(){
		$scope.achieved = false;
	}
	
	

	$scope.sign = function(){
		//报名活动
		var formDataStr = $.param({
			phone: userService.getLocalStorage("enroll-uuid"),
			aid: aid
		})
		commonService.httpReq(signUrl,formDataStr,function(response){
			if(response.STATUS == SUCCESS){
				var formDataStr = $.param({
					phone: userService.getLocalStorage("enroll-uuid")
				});

				commonService.httpReq(getTimesUrl,formDataStr,function(response){
					if(response.STATUS == SUCCESS){
						var activities = response.RESULT;
						var actList = [];
						$.each(activities.record,function(index,value){
							var temp = {};
							temp['num'] = index;
							temp['term'] = value;
							actList.push(temp);
						})
						activities.record = actList;
						$scope.activities = activities;
						$scope.pop = true;
					}else{
						alert(response.ERRMSG);
					}
				})

			}else{
				//alert(response.ERRMSG);
				$scope.achieved = true;
			}
		});
	}
})
